package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.CartM;
import com.luck.easyfoodbatch.ui.ShopDetailsActivity;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.adapter
 * email：893855882@qq.com
 * data：16/9/29
 */
public class ShopCardItemAdapter extends RecyclerView.Adapter<ShopCardItemAdapter.ShopAdapterHolder> {
    private int index = 0;
    private Context mContext;
//    public SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
    private List<CartM.DataBean.CartsBean.GoodsBean> goods = new ArrayList<>();
    private String total_price;
    public OnRefresh onRefresh;
    public OnOrderNum orderNum;

    public interface OnRefresh {
        void onRefresh(int position, boolean isChecked, String cid);
    }

    public interface OnOrderNum {
        void onOrderNumber(String cid, int num);
    }

    public ShopCardItemAdapter(Context context, int index, List<CartM.DataBean.CartsBean.GoodsBean> goods, OnRefresh onRefresh, OnOrderNum orderNum, String total_price) {
        super();
        this.mContext = context;
        this.index = index;
        this.goods = goods;
        this.onRefresh = onRefresh;
        this.orderNum = orderNum;
        this.total_price = total_price;
    }


    @Override
    public ShopCardItemAdapter.ShopAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_subclass_item, parent, false);
        return new ShopAdapterHolder(rootView);
    }


    @Override
    public void onBindViewHolder(final ShopCardItemAdapter.ShopAdapterHolder holder, final int position) {
        final CartM.DataBean.CartsBean.GoodsBean bean = goods.get(position);
        String goods_thumb = bean.getGoods_thumb();
        String goods_name = bean.getGoods_name();
        String propertyTags = bean.getPropertyTags();
        final String price = bean.getPrice();
        String due_date = bean.getDue_date();
        final String num = bean.getNum();
        if (!Tools.isNull(goods_thumb)) {
            String path = "";
            if (goods_thumb.startsWith("http")) {
                path = goods_thumb;
            } else {
                path = HttpUtil.IMG_THUMB_BASE_URL + goods_thumb;
            }
            Glide.with(holder.iv_banner.getContext())
                    .load(path)
                    .centerCrop()
                    .placeholder(R.mipmap.loading_4b3)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.iv_banner);
        } else {
            holder.iv_banner.setImageResource(R.mipmap.loading_4b3);
        }

        int enough_stock = bean.getEnough_stock();
        //enough_stock 0库存不足1充足
        switch (enough_stock) {
            case 0:
                holder.tv_enough_stock.setText("库存不足");
                break;
            case 1:
                holder.tv_enough_stock.setText("");
                break;
        }
        if (!Tools.isNull(goods_name)) {
            holder.tv_title.setText(goods_name);
        } else {
            holder.tv_title.setText("");
        }

        if (!Tools.isNull(propertyTags)) {
            holder.tv_spec.setText(propertyTags);
        } else {
            holder.tv_spec.setText("规格：");
        }

        if (!Tools.isNull(due_date)) {
            holder.tv_time.setText("保质期 至 " + Tools.SubTime2(due_date));
        } else {
            holder.tv_time.setText("保质期 至 1970-01-01");
        }

        holder.tv_number.setText(num);

        holder.tv_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.tv_number.setText("");
            }
        });
        holder.tv_number.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.ACTION_DOWN || i == EditorInfo.IME_ACTION_DONE || i == KeyEvent.KEYCODE_BACK) {
                    InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        if (textView.getText().toString().trim() == null || "".equals(textView.getText().toString().trim())) {
                            T.showToast(mContext, "数量不能为空");
                            textView.setText(1 + "");
                            orderNum.onOrderNumber(bean.getCid(), 1);
                        } else {
                            int num = Integer.parseInt(textView.getText().toString().trim());
                            if (num != 0) {
                                orderNum.onOrderNumber(bean.getCid(), num);
                            } else {
                                T.showToast(mContext, "数量不能为0");
                                textView.setText(1 + "");
                                orderNum.onOrderNumber(bean.getCid(), 1);
                            }
                        }
                        inputMethodManager.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }
                }
                return true;
            }
        });

        holder.tv_money.setText("￥" + price);
        switch (bean.getIs_check()) {
            case 0:
                holder.iv_cb.setImageResource(R.mipmap.select_def_1x);
                break;
            case 1:
                holder.iv_cb.setImageResource(R.mipmap.select_sel_1x);
                break;
        }

        holder.ll_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (bean.getIs_check()) {
                    case 0:
                        bean.setIs_check(1);
                        onRefresh.onRefresh(index, true, bean.getCid());
                        break;
                    case 1:
                        bean.setIs_check(0);
                        onRefresh.onRefresh(index, false, bean.getCid());
                        break;
                }
                notifyDataSetChanged();
            }
        });
        holder.ll_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("gid", bean.getGid() + "");
                intent.putExtras(bundle);
                intent.setClass(mContext, ShopDetailsActivity.class);
                mContext.startActivity(intent);
            }
        });
        holder.ll_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number = holder.tv_number.getText().toString().trim();
                if (number.equals("") || number.length() <= 0) {
                    T.showToast(mContext, "数量不能为空");
                    holder.tv_number.setText(1 + "");
                    orderNum.onOrderNumber(bean.getCid(), 1);
                } else {
                    int num = Integer.parseInt(number);
                    num++;
                    holder.tv_number.setText(num + "");
                    orderNum.onOrderNumber(bean.getCid(), num);
                }
            }
        });
        holder.ll_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number = holder.tv_number.getText().toString().trim();
                if (number == null || number.length() <= 0) {
                    T.showToast(mContext, "数量不能为空");
                    holder.tv_number.setText(1 + "");
                    orderNum.onOrderNumber(bean.getCid(), 1);
                } else {
                    int num = Integer.parseInt(number);
                    if (num != 0) {
                        if (num > 1) {
                            num--;
                        } else {
                            return;
                        }
                        holder.tv_number.setText(num + "");
                        orderNum.onOrderNumber(bean.getCid(), num);
                    } else {
                        T.showToast(mContext, "数量不能为0");
                        holder.tv_number.setText(1 + "");
                        orderNum.onOrderNumber(bean.getCid(), 1);
                    }
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return goods.size();
    }

//    @Override
//    public int getSwipeLayoutResourceId(int position) {
//        return R.id.swipelayout;
//    }
//
//    @Override
//    public void notifyDatasetChanged() {
//        super.notifyDataSetChanged();
//    }
//
//    @Override
//    public void openItem(int position) {
//        mItemManger.openItem(position);
//    }
//
//    @Override
//    public void closeItem(int position) {
//        mItemManger.closeItem(position);
//    }
//
//    @Override
//    public void closeAllExcept(SwipeLayout layout) {
//        mItemManger.closeAllExcept(layout);
//    }
//
//    @Override
//    public void closeAllItems() {
//        mItemManger.closeAllItems();
//    }
//
//    @Override
//    public List<Integer> getOpenItems() {
//        return mItemManger.getOpenItems();
//    }
//
//    @Override
//    public List<SwipeLayout> getOpenLayouts() {
//        return mItemManger.getOpenLayouts();
//    }
//
//    @Override
//    public void removeShownLayouts(SwipeLayout layout) {
//        mItemManger.removeShownLayouts(layout);
//    }
//
//    @Override
//    public boolean isOpen(int position) {
//        return mItemManger.isOpen(position);
//    }
//
//    @Override
//    public Attributes.Mode getMode() {
//        return mItemManger.getMode();
//    }
//
//    @Override
//    public void setMode(Attributes.Mode mode) {
//        mItemManger.setMode(mode);
//    }


    public class ShopAdapterHolder extends RecyclerView.ViewHolder {
        ImageView iv_banner, iv_cb;
        EditText tv_number;
        TextView tv_title, tv_spec, tv_money, tv_time, tv_enough_stock;
        LinearLayout ll_cb, ll_add, ll_sub, ll_goods;

        public ShopAdapterHolder(View view) {
            super(view);
            ll_goods = (LinearLayout) view.findViewById(R.id.ll_goods);
            ll_cb = (LinearLayout) view.findViewById(R.id.ll_cb);
            iv_cb = (ImageView) view.findViewById(R.id.iv_cb);
            tv_number = (EditText) view.findViewById(R.id.tv_number);
            ll_sub = (LinearLayout) view.findViewById(R.id.ll_sub);
            ll_add = (LinearLayout) view.findViewById(R.id.ll_add);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_spec = (TextView) view.findViewById(R.id.tv_spec);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            iv_banner = (ImageView) view.findViewById(R.id.iv_banner);
            tv_enough_stock = (TextView) view.findViewById(R.id.tv_enough_stock);
        }
    }

}
